//1 声明变量用来缓存组件对象
var systemDatagrid, systemDialog, systemDialogForm, systemSearchDialog, systemSearchForm;
$(function () {
  //2 使用声明的变量来缓存组件
  systemDatagrid = $("#systemDatagrid");
  systemDialog = $("#systemDialog");
  systemDialogForm = $("#systemDialogForm");
  systemSearchDialog = $("#systemSearchDialog");
  systemSearchForm = $("#systemSearchForm");
  //3 创建一个命令对象，存放所有的按钮的点击事件的回调
  cmdObj = {
    systemAdd: function () {
      //清空表单
      systemDialogForm.form("reset");
      systemDialog.dialog("open").dialog("setTitle", "添加系统").dialog("center");
    },
    systemEdit: function () {
      //获取选中行
      var row = systemDatagrid.datagrid("getSelected");
      //console.debug(row);
      //判断是否选中
      if (!row) {
        $.messager.alert("提示", "请选中一行修改", "info");
        return;
      }
      //清空form表单
      systemDialogForm.form("clear");
      //回显form表单
      systemDialogForm.form("load", row);
      //不回显状态，因为有专门的按钮来修改状态，且添加和修改表单用的为同一个
      systemDialog.dialog("open").dialog("setTitle", "修改系统").dialog("center");
    },
    systemDelete: function () {
      var row = systemDatagrid.datagrid("getSelected");
      if (!row) {
        $.messager.alert("提示", "请选中一行数据删除", "error");
        return;
      }
      //提示是否删除
      $.messager.confirm("提示", "确认删除吗", function (r) {
        if (r) {
          //发送ajax请求删除
          $.get("system/delete", {id: row.id}, function (result) {
            if (result.success) {
              //成功
              $.messager.alert("提示", "删除成功", "info");
              //刷新表格
              systemDatagrid.datagrid("reload");
            } else {
              //给出失败的提示
              $.messager.alert("提示", result.errorCode + "--" + result.message, "error");
            }
          })
        }
      })
    },
    systemRefresh: function () {
      $("#searchQ").textbox("setValue", "");
      systemDatagrid.datagrid("reload");
    },
    // systemChange:function () {
    //   var row = systemDatagrid.datagrid("getSelected");
    //   if(!row){
    //     $.messager.alert("提示","请选中一行数据修改状态","info");
    //     return;
    //   }
    //   $.get("system/change",{id:row.id},function (r) {
    //     if(r){
    //       systemDatagrid.datagrid("reload");
    //     }else{
    //       $.messager.alert("提示","更改失败！--"+r.message,"error")
    //     }
    //   })
    // },
    systemSave: function () {
      //发送ajax请求
      systemDialogForm.form('submit', {
        url: "system/save",
        onSubmit: function (param) {//提交之前的回调函数
          //do some check
          return systemDialog.form("validate");
        },
        success: function (data) {
          //alert(data);
          var result = $.parseJSON(data);
          //console.debug(result.success);
          if (result.success) {
            //成功
            //关闭模态框
            systemDialog.dialog("close");
            //给出提示
            $.messager.alert("温馨提示", result.message, "info");
            //刷新表格数据
            systemDatagrid.datagrid("reload");
          } else {
            //给出失败的提示
            $.messager.alert("温馨提示", result.errorCode + "-" + result.message, "error");
          }
        }
      })
    },
    systemCancel: function () {
      systemDialog.dialog("close");
    },
    search: function () {
      // var param = $("#searchForm").serialize();
      // console.debug(param);

      // var param = $("#searchForm").serializeArray();
      // console.debug(param);

      // var param = $("#searchForm").serializeJSON();
      // console.debug(param);            //Object { q: "系统1" }
      systemDatagrid.datagrid("load", $("#searchForm").serializeJSON())
    },
    searchAdvance: function () {
      systemSearchForm.form("clear");
      systemSearchDialog.dialog("open").dialog("center");
    },
    searchCommit: function () {
      //封装参数
      var param = systemSearchForm.serializeJSON();
      //console.debug(param);
      //关闭模态框
      systemSearchDialog.dialog("close");
      // 重新加载数据表格
      systemDatagrid.datagrid("load", param);
    },
    searchCancel: function () {
      systemSearchDialog.dialog("close");
    }
  }

  //4 初始化组件
  //4.1 初始化数据表格
  systemDatagrid.datagrid({
    title: '系统列表',
    url: 'system/page',
    pagination: true,
    rownumbers: true,
    singleSelect: true,
    fit: true,
    pageList: [10, 15, 20],
    pageSize: 10,
    fitColumns: true,//列宽度
    columns: [[
      {field: 'identification', title: '标识', width: 100},
      {field: 'name', title: '系统名称', width: 100},
      {field: 'scode', title: '编码', width: 100},
      {field: 'version', title: '版本', width: 100}
    ]],
    toolbar: '#systemDatagridToolbar',
  });
  // systemDatagrid.datagrid('getPager').pagination({
  //   onSelectPage:function(pageNumber,pageSize) {
  //     $.ajax({
  //       url: "system/page",
  //       method: "get",
  //       success: function (data) {
  //         var param = {
  //           total: data.totalElements,
  //           rows: data.content
  //         }
  //         console.log(param);
  //         systemDatagrid.datagrid("load",param);
  //       }
  //     })
  //   })

  //4.2 初始化添加修改模态框
  systemDialog.dialog({
    width: 400,
    height: 200,
    closed: true,//默认关闭
    modal: true,//模态框
    pagination: true,
    buttons: "#systemDialogToolbar"
  });

  //4.3初始化高级查询模态框
  systemSearchDialog.dialog({
    title: "高级查询",
    iconCls: "icon-search",
    width: 400,
    height: 200,
    closed: true,
    modal: true,
    buttons: "#systemsSearchDialogToolbar"
  });
  //5 为页面上的所有的a标签注册点击事件
  $("a").on("click", function () {
    //获取标签的data-cmd属性值
    var cmd = $(this).data("cmd");
    if (cmd) {
      cmdObj[cmd + ""]();
    }
  });
});
